package com.dji.GSDemo.GaodeMap;

import dji.common.gimbal.GimbalMode;
import dji.common.gimbal.Rotation;
import dji.common.gimbal.RotationMode;
import dji.common.util.CommonCallbacks;
import dji.sdk.base.BaseProduct;
import dji.sdk.gimbal.Gimbal;
import dji.sdk.products.Aircraft;

public class GimbalController {
    private static class Singleton {
        private static final GimbalController instance = new GimbalController();
    }

    public static GimbalController getInstance() {
        return GimbalController.Singleton.instance;
    }

    private GimbalController() {

    }

    private Gimbal gimbal = null;

    public void updateProduct() {
        BaseProduct product = DJIDemoApplication.getProductInstance();
        if (product != null && product.isConnected()) {
            if (product instanceof Aircraft) {
                gimbal = ((Aircraft) product).getGimbal();
            }
        }
        if (gimbal != null) {
            gimbal.setMode(GimbalMode.FREE, null);
        }
    }

    public boolean isGimbalExist(){
        return gimbal != null;
    }

    private Gimbal getGimbal() {
        return gimbal;
    }

    public void rotate(Rotation rotation, CommonCallbacks.CompletionCallback callback) {
        Gimbal g = getGimbal();
        if (g != null) {
            g.rotate(rotation, callback);
        }
    }

    public void rotateAbsoluteAngle(int yaw, int pitch, int roll, CommonCallbacks.CompletionCallback callback) {
//        Rotation.Builder rotationBuilder = new Rotation.Builder();
        Rotation.Builder rotationBuilder = new Rotation.Builder().mode(RotationMode.RELATIVE_ANGLE).pitch(pitch).yaw(Rotation.NO_ROTATION). roll(Rotation.NO_ROTATION).time(2);
        Rotation rotation = rotationBuilder.build();
        rotate(rotation, callback);
    }

    public void reset() {
        Gimbal g = getGimbal();
        g.reset(null);
    }
}
